//
//  TKIMRequestService.m
//  TKIM_首创
//
//  Created by macpro on 15/12/14.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMRequestService.h"
#import "TKIMMenuBar.h"
#import "TKIMContact.h"

@interface TKIMRequestService()
{
     TKIMNetHelper * _connect;
}
@end

@implementation TKIMRequestService

-(instancetype)init
{
    if (self = [super init]) {
        _connect = [[TKIMNetHelper alloc] init];
    }
    return self;
}

/**
 *  公众号子菜单
 *
 *  @param username 公众号的userid
 *  @param callBack 数据请求后的回调
 */
-(void)getOfficialMenusWithUsername:(NSString *)username callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo *requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary *dic  = [[NSMutableDictionary alloc] init];
    dic[@"funcNo"] = @"1003013";
    dic[@"username"] = username;
    requestVo.param = dic;

    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        if (resultVo.errorNo == 0)
        {
            NSMutableArray *results = [NSMutableArray array];
            NSArray *array = (NSArray *)resultVo.results;
            NSString * menusString = array[0][@"menus"];
            NSArray * menusArray=(NSArray *)[TKIMDataHelper stringToJson:menusString];
            for (NSDictionary * dic in menusArray) {
                TKIMMenuBarItem * item=[[TKIMMenuBarItem alloc] init];
                [item setValuesForKeysWithDictionary:dic];
                [results addObject:item];
            }
            resultVo.results = results;
        }
        callBack(resultVo);
    }];
}


/**
 *  @author zrs, 15-12-21 11:12:07
 *
 *  @brief 获得app最新版本信息
 *
 *  @param callBack 回调
 */
-(void)getNewestVersionCallBack:(TKIMCallBack)callBack
{
//    TKIMRequestVo *requestVo = [[TKIMRequestVo alloc] init];
//    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
//    NSMutableDictionary *dic  = [[NSMutableDictionary alloc] init];
//    dic[@"funcNo"] = @"1003014";
//    dic[@"type"] = @"2";         //代表iOS
//    requestVo.param = dic;
//    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
//        if (resultVo.errorNo==0) {
//            NSArray *array = (NSArray *)resultVo.results;
//            NSDictionary * dic = nil;
//            if (array.count>0) {
//                dic = array[0];
//            }
//            TKIMVersionModel * model = [[TKIMVersionModel alloc] init];
//            [model setValuesForKeysWithDictionary:dic];
//            resultVo.results = model;
//        }
//        callBack(resultVo);
//    }];
}


/**
 *  @author zrs, 15-12-21 11:12:25
 *
 *  @brief 用户发送意见反馈
 *
 *  @param username 用户名
 *  @param message  用户意见
 *  @param callBack 发送的回调
 */
-(void)sendFeedBackMessageWithUsername:(NSString *)username content:(NSString *)message callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003015";
    dic[@"username"] = username;
    dic[@"content"] = message;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-12-29 11:12:53
 *
 *  @brief 手机联系人角色查询
 *
 *  @param username       用户名
 *  @param contactsMobile 所有手机联系人电话号码信息  中间用,分隔
 *  @param callBack       回调
 */
-(void)searchContactRoleWithUsername:(NSString *)username contactsMobile:(NSString *)contactsMobile callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003020";
    dic[@"username"] = username;
    dic[@"contacts"] = contactsMobile;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-02-29 10:02:17
 *
 *  @brief 查询好友资料(带缓存)
 *
 *  @param user
 *  @param callBack
 */
-(void)viewFriendInformationWithUser:(TKIMUser *)user callBack:(TKIMCallBack)callBack
{
//    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
//    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
//    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
//    dic[@"funcNo"] = @"1003120";
//    dic[@"friendname"] = user.userID;
//    dic[@"username"] = [TKIMSDKHelper getLoginUserName];
//    dic[@"sessionkey"] = [TKIMCacheHelper valueForKey:TKIMHttpToken];
//    requestVo.param = dic;
//    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
//        
//        if (resultVo.errorNo==0) {
//            NSArray * array = (NSArray *)resultVo.results;
//            if (array.count>0) {
//                NSDictionary * dic = array[0];
//                TKIMFriend * fri = [[TKIMFriend alloc] init];
//                fri.userDescription = user.userDescription;
//                fri.thumbPhotoUrl = user.thumbPhotoUrl;
//                fri.originalPhotoUrl = user.originalPhotoUrl;
//                fri.userID = user.userID;
//                fri.nickName = user.nickName;
//                fri.userType = user.userType;
//                fri.remark = user.remark;
//                
//                [fri setValuesForKeysWithDictionary:dic];
//                resultVo.results = fri;
//            }
//        }
//        callBack(resultVo);
//    }];
//    
    
    
    
    
    
    
}

/**
 *  @author zhoufz, 16-07-18 13:07:33
 *
 *  @brief 查询好友资料（没缓存）
 *
 *  @param user
 *  @param callBack
 */
-(void)viewFriendInformationWithoutCacheWithUser:(TKIMUser *)user callBack:(TKIMCallBack)callBack
{
//    TKIMRequestVo *requestVo = [[TKIMRequestVo alloc] init];
//    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    dic[@"funcNo"] = @"1003120";
//    dic[@"friendname"] = user.userID;
//    dic[@"username"] = [TKIMSDKHelper getLoginUserName];
//    dic[@"sessionkey"] = [TKIMCacheHelper valueForKey:TKIMHttpToken];
//    requestVo.param = dic;
//    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
//        if (resultVo.errorNo==0) {
//            NSArray * array = (NSArray *)resultVo.results;
//            if (array.count>0) {
//                NSDictionary * dic = array[0];
//                TKIMFriend * fri = [[TKIMFriend alloc] init];
//                fri.userDescription = user.userDescription;
//                fri.thumbPhotoUrl = user.thumbPhotoUrl;
//                fri.originalPhotoUrl = user.originalPhotoUrl;
//                fri.userID = user.userID;
//                fri.nickName = user.nickName;
//                fri.userType = user.userType;
//                fri.remark = user.remark;
//                
//                [fri setValuesForKeysWithDictionary:dic];
//                resultVo.results = fri;
//            }
//        }
//        callBack(resultVo);
//
//    }];
}

/**
 *  @author zhoufz, 16-07-18 14:07:38
 *
 *  @brief 查询好友标签
 *
 *  @param user
 *  @param friendUser
 *  @param callBack
 */
-(void)viewFriendLabelWithLoginUser:(TKIMUser *)user andFriendUser:(TKIMUser *)friendUser callBack:(TKIMCallBack)callBack
{
//    TKIMRequestVo *requestVo = [[TKIMRequestVo alloc] init];
//    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    dic[@"funcNo"] = @"1003128";
//    dic[@"username"] = user.userID;
//    dic[@"friendname"] = friendUser.userID;
//    requestVo.param = dic;
//    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
//        NSMutableArray *labelsM = [NSMutableArray array];
//        if (resultVo.errorNo == 0) {
//            NSArray *labelArray = (NSArray *)resultVo.results;
//            for (NSDictionary *labelDic in labelArray) {
//                NSString *labelString = labelDic[@"labels"];
//                NSArray * oneLabels = (NSArray *)[TKIMDataHelper stringToJson:labelString];
//                for (NSDictionary * dic in oneLabels) {
//                    TKIMPersonaImperssItem * item = [TKIMPersonaImperssItem personalImpressItemWithDic:dic];
//                    [labelsM addObject:item];
//                }
//            }
//
//        }
//        resultVo.results = labelsM;
//        callBack(resultVo);
//    }];
}

/**
 *  @author zhengrs, 16-02-29 17:02:44
 *
 *  @brief 给朋友点赞
 *
 *  @param friendName 朋友的名字
 *  @param callBack
 */
-(void)thumbUpWithFriendName:(NSString *)friendName callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003122";
    dic[@"username"] = [TKIMSDKHelper getLoginUserName];
    dic[@"friendname"] = friendName;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-02-29 18:02:30
 *
 *  @brief 给投顾或经理贴标签
 *
 *  @param friendName    对方的名字
 *  @param labelidString  标签的id 多个标签中间用,隔开
 *  @param callBack
 */
-(void)labelingWithUserName:(NSString *)username FriendName:(NSString *)friendName labelid:(NSString *)labelidString callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003121";
    dic[@"username"] = username;
    dic[@"friendname"] = friendName;
    dic[@"labelid"] = labelidString;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-03-01 15:03:43
 *
 *  @brief 查询所有可贴标签
 */
-(void)searchAllAvailabelLabelsCallBack:(TKIMCallBack)callBack;
{
//    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
//    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
//    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
//    dic[@"funcNo"] = @"1003124";
//    dic[@"sessionkey"] = [TKIMCacheHelper valueForKey:TKIMHttpToken];
//    requestVo.param = dic;
//    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
//        if (resultVo.errorNo==0) {
//            NSMutableArray * resultArray = [NSMutableArray array];
//            
//            NSArray * array = (NSArray *)resultVo.results;
//            
//            for (NSDictionary * dic in array) {
//                TKIMPersonaImperssItem * item = [[TKIMPersonaImperssItem alloc] init];
//                item.labelid = [dic[@"lableid"] integerValue];
//                item.personaImperss = dic[@"labletitle"];
//                item.isSelected = NO;
//                [resultArray addObject:item];
//            }
//            resultVo.results = resultArray;
//        }
//        callBack(resultVo);
//    }];
    
}

/**
 *  @author zhengrs, 16-03-01 19:03:57
 *
 *  @brief 评分
 *
 *  @param friendName 对方的名字
 *  @param score      评分数
 */
-(void)scoreWithFriendName:(NSString *)friendName score:(NSInteger)score callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003123";
    dic[@"friendname"] = friendName;
    dic[@"username"] = [TKIMSDKHelper getLoginUserName];
    dic[@"score"] = @(score);
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-03-05 14:03:18
 *
 *  @brief 修改好友的备注
 *
 *  @param friendName 好友对象
 *  @param nickname   修改的备注
 *  @param callBack
 */
-(void)modifyFriendRemarkWithFriendName:(TKIMUser *)user remark:(NSString *)remark callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003029";
    dic[@"username"] = [TKIMSDKHelper getLoginUserName];
    dic[@"friendname"] = user.userID;
    dic[@"remark"] = remark;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        if (resultVo.errorNo==0) {
            TKIMUser * oldUser = [[TKIMDBManager shareInstance] queryAHisUserByUserId:user.userID];
            oldUser.remark = remark;
            [[TKIMDBManager shareInstance] updateUser:oldUser];
        }
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-03-07 14:03:27
 *
 *  @brief 添加好友到新分组
 *
 *  @param username   用户名
 *  @param friendName 朋友名
 *  @param groupName  新的分组名
 *  @param callBack
 */
-(void)addFriendToNewGroupWithUsername:(NSString *)username friend:(TKIMUser *)friend groupName:(NSString *)groupName callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003030";
    dic[@"username"] = username;
    dic[@"friendname"] = friend.userID;
    dic[@"groupname"] = groupName;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        if (resultVo.errorNo==0) {
            NSArray * array = (NSArray *)resultVo.results;
            if (array.count>0) {
                NSDictionary * dic = [array firstObject];
                TKIMUserGroup * group = [[TKIMUserGroup alloc] init];
                group.groupId = dic[@"id"];
                group.groupName = dic[@"groupname"];
                group.canDelete = [dic[@"candelete"] boolValue];
                group.canModify = [dic[@"canmodify"] boolValue];
                group.rank = [dic[@"rank"] integerValue];
                group.users = [NSMutableArray arrayWithObject:friend];
                //插入组
                [[TKIMDBManager shareInstance] insertUserGroup:group];
                
                //将用户插入组
                friend.groupId = group.groupId;
                friend.groupName = group.groupName;
                [[TKIMDBManager shareInstance] insertUser:friend];
                
                resultVo.results = group;
            }
        }
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-03-07 16:03:32
 *
 *  @brief 添加好友到指定组
 *
 *  @param username 用户名
 *  @param friend   好友对象
 *  @param groupId  指定组ID
 */
-(void)addFriendToDesignGroupWithUsername:(NSString *)username friend:(TKIMUser *)friend designGroup:(TKIMUserGroup *)designGroup callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003031";
    dic[@"username"] = username;
    dic[@"friendname"] = friend.userID;
    dic[@"id"] = designGroup.groupId;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        
        if (resultVo.errorNo==0) {
            friend.groupId = designGroup.groupId;
            friend.groupName = designGroup.groupName;
            [[TKIMDBManager shareInstance] insertUser:friend];
        }
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-03-07 16:03:32
 *
 *  @brief 删除好友从指定组
 *
 *  @param username 用户名
 *  @param friend   好友对象
 *  @param groupId  指定组ID
 */
-(void)removeFriendFromDesignGroupWithUsername:(NSString *)username friend:(TKIMUser *)friend designGroup:(TKIMUserGroup *)designGroup callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003032";
    dic[@"username"] = username;
    dic[@"friendname"] = friend.userID;
    dic[@"id"] = designGroup.groupId;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        
//        if (resultVo.errorNo==0) {
//            friend.groupId = designGroup.groupId;
//            friend.groupName = designGroup.groupName;
//            [[TKIMDBManager shareInstance] removeUser:friend];
//        }
        
        callBack(resultVo);
    }];
}

/**
 *  @author zhengrs, 16-03-05 13:03:12
 *
 *  @brief 群发消息
 *
 *  @param target  发送的对象userID用,隔开
 *  @param message 消息
 *  @param type    消息类型
 *  @param callBack
 */
-(void)sendGroupMessageToSpecialFriends:(NSString *)target message:(TKIMMessage *)message callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo * requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [[TKIMSDKConfig shareInstance] generalUrl];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"funcNo"] = @"1003125";
    dic[@"username"] = [TKIMSDKHelper getLoginUserName];
    dic[@"target"] = target;
    NSMutableDictionary * contentDic = [NSMutableDictionary dictionary];
    
    if (message.type==TKIMMsgTypeTxt) {//普通文本
        contentDic[@"msg"] = message.text;
        contentDic[@"type"] = @"txt";
        
    }else if (message.type== TKIMMsgTypeImage){//发送图片
        TKIMImageMessage * imgMsg = (TKIMImageMessage *)message;
        contentDic[@"secret"] = imgMsg.secret;
        contentDic[@"thumb_secret"] = imgMsg.thumbSecret;
        contentDic[@"type"] = @"img";
        contentDic[@"url"] = imgMsg.remoteUrl;
        contentDic[@"filename"] = imgMsg.fileName;
        contentDic[@"thumb"]=imgMsg.remoteThumbUrl;
        contentDic[@"size"] = @{@"width":@(imgMsg.imageSize.width),@"height":@(imgMsg.imageSize.height)};
        
    }else if (message.type == TKIMMsgTypeAudio){//发送录音
        TKIMAudioMessage * audio = (TKIMAudioMessage *)message;
        contentDic[@"type"] = @"audio";
        contentDic[@"secret"] = audio.secret;
        contentDic[@"url"] = audio.remoteUrl;
        contentDic[@"file_length"] = audio.fileSize;
        contentDic[@"filename"] = audio.fileName;
        contentDic[@"length"] = @(audio.audioDuration);
    }
    
    dic[@"content"] = contentDic;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];
}



/**
 *  @Author lpm, 15-05-11 11:05:06
 *
 *  获得当前登录用户信息
 *
 *  @param tokenString
 */
-(void)getLoginUserInfoWithUserName:(NSString *)userName callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo *requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [TKIMSDKConfig shareInstance].generalUrl;
    NSMutableDictionary *dic  = [[NSMutableDictionary alloc] init];
    dic[@"funcNo"] = @"1003027";
    dic[@"username"] = userName;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo)
     {
         if (resultVo.errorNo == 0)
         {
             @try
             {
                 NSArray *arr = (NSArray *)resultVo.results;
                 NSDictionary *dic = [arr lastObject];
                 TKIMUser *user = [[TKIMUser alloc] init];

                 if (dic)
                 {
                     user.userName = dic[@"userName"];
                     user.userType = [dic[@"usertype"] intValue];
                 }
                 resultVo.results = user;
             }
             @catch (NSException *exception)
             {
                 NSLog(@"%@",exception.description);
             }
         }
         callBack(resultVo);
     }];
}

/**
 *  @author zhoufz, 16-05-26 18:05:02
 *
 *  @brief 移动分组
 *
 *  @param groupId (分组中所有id字符串，示例：0,2031,1102,3234)
 *  @param callBack
 */
-(void)moveGroupWithGroupId:(NSString *)groupId callBack:(TKIMCallBack)callBack
{
    TKIMRequestVo *requestVo = [[TKIMRequestVo alloc] init];
    requestVo.url = [TKIMSDKConfig shareInstance].generalUrl;
    NSMutableDictionary *dic  = [[NSMutableDictionary alloc] init];
    dic[@"funcNo"] = @"1003038";
    dic[@"ids"] = groupId;
    requestVo.param = dic;
    [_connect post:requestVo callBack:^(TKIMResultVo *resultVo) {
        callBack(resultVo);
    }];

}


+ (void) getEbondOfidWithTargetId:(NSString *)targetId targetName:(NSString *)targetName appid:(NSString *)appid callBack:(void(^)(NSData *data, NSError *error))callBack
{
    NSString *funcNo = @"5000006";
    NSString *appkey = @"ebondService";
    NSString *urlStr = [[TKIMSDKConfig shareInstance]
    getConfigByKey:@"sj1generalUrl"];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSString *args = [NSString stringWithFormat:@"crmid=%@&appkey=%@&appid=%@&funcNo=%@", targetId, appkey, appid, funcNo];
    request.HTTPBody = [args dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        callBack(data, error);
    }];
    [sessionDataTask resume];
}




@end
